package com.polaris.live.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
import androidx.recyclerview.widget.GridLayoutManager
import com.polaris.live.adapter.GameAdapter
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.databinding.LayoutPopupChildBinding
import com.polaris.live.popup.HowPlayPopup
import com.polaris.live.resp.back_resp.GameGroupVoResp
import com.polaris.live.ui.game.GameUtils

/**
 * GameListView
 *
 * @author Created by 半仙 on 2023/11/24/024 11:20
 */
class GameListView(context: Context, attributeSet: AttributeSet? = null) :
    LinearLayout(context, attributeSet) {

    private val mBinding = LayoutPopupChildBinding.inflate(LayoutInflater.from(context), this, true)

    fun setData(gameListRespItem: GameGroupVoResp, roomId: String = "") {
        mBinding.gameName.text = gameListRespItem.name

        mBinding.btnDoubt.setOnSingleClickListener {
            HowPlayPopup.show(context, gameListRespItem.list, roomId)
        }
        mBinding.recycler.apply {
            val mAdapter = GameAdapter().apply {
                setOnItemClickListener { _, _, position ->
                    val data = setSelect(position)
                    GameUtils.showGameFragment(context, data, roomId = roomId)
                    mOnClickCallback?.invoke()
                }
                setList(gameListRespItem.list)
            }
            adapter = mAdapter
            layoutManager = GridLayoutManager(
                context,
                4,
                GridLayoutManager.VERTICAL,
                false
            )
        }
    }

    private var mOnClickCallback: (() -> Unit)? = null

    fun setOnClickCallback(l: () -> Unit) {
        mOnClickCallback = l
    }
}